package com.me.dao.normal;

import com.me.dao.IItemDao;
import com.me.model.Item;
import com.me.model.normal.NormalItem;
import com.me.utils.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collection;

public class NormalItemDao implements IItemDao<NormalItem> {
	@Override
	public void addItem(NormalItem item) {
		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
		String sql = "INSERT INTO `examsys`.`item` (`id`, `title`, `content`, `score`, `answer`, `multiple`) VALUES (?, ?, ?, ?, ?, ?);";
		
		try {
			int line = runner.update(
					sql,
					getItemCount(), item.getTitle(), Arrays.toString(item.getContents())
					, item.getScore(), item.getAnswer(), item.isMultiple()
			);
			
			if (line != 1) {
				System.out.println("添加题库出错或失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public NormalItem getItem(int id) {
		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
		String sql = "SELECT * FROM examsys.item where id = ?";

		try {
			return runner.query(sql, new BeanHandler<NormalItem>(NormalItem.class), id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}


	public void updateId(int id,int newId) {
		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
		String sql = "UPDATE `examsys`.`item` SET `id` = ? WHERE (`id` = ?);";
		try {
			int line = runner.update(
					sql, newId,id
			);

			if (line != 1) {
				System.out.println("更新题目出错或失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean update(Item item) {
		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
		String sql = "UPDATE `examsys`.`item` SET `title` = ?, `content` = ?, `score` = ?, `answer` = ?, `multiple` = ? WHERE (`id` = ?);";
		try {
			int line = runner.update(
					sql,
					item.getTitle(), Arrays.toString(item.getContents()),item.getScore(),
					item.getAnswer(),item.getMultiple(),item.getId()
			);

			if (line != 1) {
				System.out.println("更新题目出错或失败");
				return false;
			}
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return false;
	}

	@Override
	public NormalItem getItemFromUser(String userId, int itemId) {
		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
		String sql = "SELECT i.*,ui.chooseId FROM examsys.item i,examsys.user_item ui,examsys.user u where i.id = ui.itemId and u.id = ui.userId and u.id = ? and i.id = ?;";

		try {
			return runner.query(sql, new BeanHandler<NormalItem>(NormalItem.class), userId, itemId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public Collection<NormalItem> getAll() {
		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
		String sql = "SELECT * FROM examsys.item";

		try {
			return runner.query(sql, new BeanListHandler<NormalItem>(NormalItem.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public Collection<NormalItem> getAllFromUser(String userId) {
		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
		String sql = "SELECT i.*,ui.chooseId FROM examsys.item i,examsys.user_item ui,examsys.user u where i.id = ui.itemId and u.id = ui.userId and u.id = ?;";

		try {
			return runner.query(sql, new BeanListHandler<NormalItem>(NormalItem.class), userId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public Long getItemCount() {
		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
		String sql = "SELECT count(*) c FROM examsys.item";

		try {
			return runner.query(sql, new ScalarHandler<Long>());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1L;
	}
	
	@Override
	public void recordUserChoose(String userId, int itemId, int chooseId) {
		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
		String sql = "update examsys.user_item set chooseId=? where userid=? and itemid=?;";

		try {
			if (runner.update(sql, chooseId, userId, itemId) != 1) {
				System.out.println("记录题目失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void initialItem(String userId, int itemId) {
		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
		String sql = "INSERT INTO `examsys`.`user_item` (`userid`, `itemid`) VALUES (?,?);";

		try {
			if (runner.update(sql, userId, itemId) != 1) {
				System.out.println("初始化题目失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean delete(int id) {
		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
		String sql = "delete FROM examsys.item where id=?";
		try {
			return runner.update(sql, id) == 1;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
//	private Class<T> entityClass;
//
//	public NormalItemDao(Class<T> tClass) {
//		entityClass = tClass;
//	}
//
//	public void addItem(String title, String[] content, int score, String answer, boolean multiple) {
//		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
//		String sql = "INSERT INTO `examsys`.`item` (`title`, `content`, `score`, `answer`, `multiple`) VALUES (?, ?, ?, ?, ?);";
//
//		try {
//			int line = runner.update(sql, title, Arrays.toString(content), score, answer, multiple);
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//	}
//
//	public T getItem(int id) {
//		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
//		String sql = "SELECT * FROM examsys.item where id = ?";
//
//		try {
//			return runner.query(sql, new BeanHandler<T>(entityClass), id);
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//		return null;
//	}
//
//	public T getItemFromUser(Object userId, Object itemId) {
//		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
//		String sql = "SELECT i.*,ui.chooseId FROM examsys.item i,examsys.user_item ui,examsys.user u where i.id = ui.itemId and u.id = ui.userId and u.id = ? and i.id = ?;";
//
//		try {
//			return runner.query(sql, new BeanHandler<T>(entityClass), userId, itemId);
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//		return null;
//	}
//
//	public Collection<T> getAll() {
//		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
//		String sql = "SELECT * FROM examsys.item";
//
//		try {
//			return runner.query(sql, new BeanListHandler<T>(entityClass));
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//		return null;
//	}
//
//	public Collection<T> getAllFromUser(Object userId) {
//		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
//		String sql = "SELECT i.*,ui.chooseId FROM examsys.item i,examsys.user_item ui,examsys.user u where i.id = ui.itemId and u.id = ui.userId and u.id = ?;";
//
//		try {
//			return runner.query(sql, new BeanListHandler<T>(entityClass), userId);
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//		return null;
//	}
//
//	public Long getItemCount() {
//		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
//		String sql = "SELECT count(*) c FROM examsys.item";
//
//		try {
//			return runner.query(sql, new ScalarHandler<Long>());
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//		return -1L;
//	}
//
//	public void recordUserChoose(Object userId, Object itemId, int chooseId) {
//		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
//		String sql = "update examsys.user_item set chooseId=? where userid=? and itemid=?;";
//
//		try {
//			if (runner.update(sql, chooseId, userId, itemId) != 1) {
//				System.out.println("记录题目失败");
//			}
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//	}
//
//	public void initialItem(Object userId, Object itemId) {
//		QueryRunner runner = new QueryRunner(DBUtil.getDataSource());
//		String sql = "INSERT INTO `examsys`.`user_item` (`userid`, `itemid`) VALUES (?,?);";
//
//		try {
//			if (runner.update(sql, userId, itemId) != 1) {
//				System.out.println("初始化题目失败");
//			}
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//	}
}
